<template>
  <el-container class="consult-container">
    <el-header class="header">
      <div class="logo">蔬菜展示系统</div>
      <el-menu :default-active="activeIndex" mode="horizontal" @select="handleSelect">
        <el-menu-item index="home" @click="goToHome">首页</el-menu-item>
        <el-menu-item index="vegetables">蔬菜信息</el-menu-item>
        <el-menu-item index="consult">咨询页面</el-menu-item>
      </el-menu>
    </el-header>

    <el-main class="main-content">
      <div class="consult-form-container">
        <h2 class="form-title">咨询表单</h2>
        <el-card class="consult-form-card">
          <el-form ref="consultForm" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入您的姓名"></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入您的联系电话"></el-input>
            </el-form-item>
            <el-form-item label="咨询类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择咨询类型">
                <el-option label="产品咨询" value="product"></el-option>
                <el-option label="订单咨询" value="order"></el-option>
                <el-option label="配送咨询" value="delivery"></el-option>
                <el-option label="其他问题" value="other"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="咨询内容" prop="content">
              <el-input v-model="form.content" type="textarea" :rows="5" placeholder="请详细描述您的问题"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('consultForm')">提交咨询</el-button>
              <el-button @click="resetForm('consultForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </el-main>

    <el-footer class="footer">
      <p>蔬菜展示系统</p>
    </el-footer>
  </el-container>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const router = useRouter();
const consultForm = ref(null);

// 导航状态
const activeIndex = reactive('consult');

// 表单数据
const form = reactive({
  name: '',
  phone: '',
  type: '',
  content: ''
});

// 表单验证规则
const rules = reactive({
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择咨询类型', trigger: 'change' }
  ],
  content: [
    { required: true, message: '请输入咨询内容', trigger: 'blur' },
    { min: 10, message: '咨询内容至少10个字符', trigger: 'blur' }
  ]
});

// 返回首页
const goToHome = () => {
  router.push('/home');
};

// 导航选择处理
const handleSelect = (key) => {
  if (key === 'home') {
    goToHome();
  }
};

// 提交表单
const submitForm = (formName) => {
  consultForm.value.validate((valid) => {
    if (valid) {
      // 这里可以添加提交到后端的逻辑
      ElMessage.success('咨询提交成功，我们将尽快与您联系！');
      consultForm.value.resetFields();
    } else {
      ElMessage.error('表单验证失败，请检查输入内容');
      return false;
    }
  });
};

// 重置表单
const resetForm = (formName) => {
  consultForm.value.resetFields();
};
</script>

<style scoped lang="scss">
.consult-container {
  height: 100vh;
}

.header {
  background-color: #409EFF;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;

  .logo {
    font-size: 24px;
    font-weight: bold;
  }
}

.main-content {
  padding: 20px;
  background-color: #f5f5f5;
}

.consult-form-container {
  max-width: 800px;
  margin: 0 auto;
}

.form-title {
  font-size: 24px;
  margin-bottom: 20px;
  color: #333;
  text-align: center;
}

.consult-form-card {
  padding: 30px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.footer {
  text-align: center;
  background-color: #409EFF;
  color: white;
  padding: 10px 0;
}
</style>